package day1105.second;

import day1105.common.MyUtil;
import day1105.common.Student;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static day1105.common.MyUtil.printLine;

/**
 * @author Mr.ma
 * @since 2022/11/6 17:01
 **/
public class CollectTest {
    public static void main(String[] args) {
        List<Student> studentList = MyUtil.createStudentList();
        studentList.stream().forEach(System.out::println);
        printLine("年龄最大的学生");
        Student student = studentList.stream().collect(Collectors.maxBy(Student::compareTo)).get();
        System.out.println("student = " + student);
        printLine("成绩总和");
        Double collect = studentList.stream().collect(Collectors.summingDouble(Student::getScore));
        System.out.println("collect = " + collect);
        printLine("按班级、城市分组");
        Map<String, Map<String, List<Student>>> collect1 = studentList.stream().collect(Collectors.groupingBy(Student::getClazz, Collectors.groupingBy(Student::getCity)));
        collect1.forEach((key,value) -> {
            System.out.println(key + ": ");
            value.forEach((k,v) ->{
                System.out.println(k + ": " + v);
            });
        });
        printLine("获取全部学生姓名（按年龄排序，从小到大）");
        String collect2 = studentList.stream().sorted().map(Student::getName).collect(Collectors.joining(","));
        System.out.println("collect2 = " + collect2);
    }
}
